import { Controller, Get, Post, Body, Patch, Param, Request, Response, Query, Headers, HttpCode, Delete, Version, Header, Session, Inject } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { RegisterUserDto } from './dto/register-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { LoginUser } from './dto/login-user.dto'
import { AllowAnon } from '../common/decorators/allow-anon.decorator'
import * as svgCaptcha from 'svg-captcha'
import { query } from 'express';


@Controller('user')
export class UserController {
  constructor(
    private readonly userService: UserService
  ) {}


  @Post('/add/tags')
  addTags(@Body() params:{tags:string[],userId:number}){
    return this.userService.addTags(params)
  }

  @Get('/get/tags')
  getTags(@Query() params:{userId:number}){
    return this.userService.getTagsByUserId(params.userId)
  }
 
  @Post('addUser')
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }
  
  @AllowAnon()
  @Post('register')
  async register(@Body() registerUserDto: RegisterUserDto) {
    return await this.userService.register(registerUserDto);
  }

  @AllowAnon()
  @Post('login')
  async login(@Body() dto:LoginUser,@Headers() headers,@Session() session){
      console.log(dto,session.code);
      if(session.code.toLocaleLowerCase() === dto?.verifyCode?.toLocaleLowerCase()){
        return await this.userService.login(dto.name, dto.password)
      }else{
        return {
          code:200,
          message:"验证码错误"
        }
      }
      
  }
 
  // @AllowAnon()
  @Get()
  findAll(@Query() query:{keyWord:string,page:number,pageSize:number}) {
    return this.userService.findAll(query);
  }
  
  @AllowAnon()
  @Get('code')
  createCode(@Request() req,@Response() res,@Session() session){
    const captcha = svgCaptcha.create({
      size: 4,//生成几个验证码
      fontSize: 50, //文字大小
      noise: 2, // 干扰线条数
      width: 150,  //宽度
      height: 40,  //高度
      background: '#fff',  //背景颜色
    })
    req.session.code = captcha.text //存储验证码记录到session
    res.type('image/svg+xml')
    res.send(captcha.data)
  }
 
 
  @Patch(':id')
  update(@Param('id') id: number, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }
 
  @Delete(':id')
  remove(@Param('id') id: number) {
    return this.userService.remove(+id);
  }
}








/*
@Controller("user")
// @Controller({
//   path:"user",
//   version:"1"
// })
export class UserController {
  constructor(private readonly userService: UserService) {}
  
 
  @Post()
  // create(@Body('age') age) {
  //   console.log(age);
  //   return {
  //     code:200,
  //     message:age
  //   }
  // }
  // create(@Body() body) {
  //   console.log(body);
  //   return {
  //     code:200,
  //     message:body.name
  //   }
  // }
  // create(@Request() req) {
  //   console.log(req.body);
  //   return {
  //     code:200,
  //     message:req.body.name
  //   }
  // }
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @Get(':id') //动态参数
  // @HttpCode(500)
  // findId(@Param('id') param,@Headers() headers){
  //   console.log(param);
  //   console.log(headers);
  //   return {
  //     code:200
  //   }
  // }
  @Get(':id')
  findId(@Param() param){
    console.log(param);
    return {
      code:200
    }
  }
  // @Get(':id')
  // findId(@Request() req){
  //   console.log(req.params);
  //   return {
  //     code:200
  //   }
  // }

  @Get()
  // // @Version("1")
  // findAll(@Query() query) {
  //   console.log(query);
  //   return {
  //     code:200,
  //     message:query.name
  //   }
  //   // return this.userService.findAll();
  // }
  findAll(@Request() req) {
    console.log(req.query);
    // return {
    //   code:200,
    //   message:req.query.name
    // }
    return this.userService.findAll();
  }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.userService.findOne(+id);
  // }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
}*/